export function traversalMenu(arr) {
  function traverse(node) {
    if (!node) {
      return
    }
    set.add(node.url)
    power[node.url] = {}
    if (node.operateResources && node.operateResources.length > 0) {
      node.operateResources.forEach((item) => {
        power[node.url][item.code] = item.name
      })
    }
    if (node.childMenu && node.childMenu.length > 0) {
      let i = 0
      for (i = 0; i < node.childMenu.length; i++) {
        traverse(node.childMenu[i])
      }
    }
  }
  const set = new Set(),
    power = {}
  arr.forEach((item) => {
    traverse(item)
    set.add(item.url)
  })
  set.delete(null)
  set.delete('')
  return {
    url: [...set],
    power
  }
}

// 获取地址栏的参数
export function getUrl(params) {
  var after = window.location.href.split('?')[1]
  if (after) {
    var reg = new RegExp('(^|&)' + params + '=([^&]*)(&|$)'),
      r = after.match(reg)
    if (r) {
      return decodeURIComponent(r[2])
    } else {
      return null
    }
  }
}

// TODO 流文件 下载
export function ExportFileBOLD(res, name) {
  const fileName = name
  var blob = new Blob([res], {
    type: 'application/vnd.ms-excel'
  })
  const URL = window.URL || window.webkitURL,
    downloadElement = document.createElement('a'),
    href = URL.createObjectURL(blob) // 创建下载的链接
  downloadElement.href = href
  downloadElement.download = fileName // 下载后文件名
  document.body.appendChild(downloadElement)
  downloadElement.click() // 点击下载
  document.body.removeChild(downloadElement) // 下载完成移除元素
  URL.revokeObjectURL(href) // 释放掉blob对象
}

// 下载blob文件
export function downloadBlob(data, name) {
  // console.log(data)
  if (!data) {
    return
  }
  const objectUrl = URL.createObjectURL(data), // 创建URL
    link = document.createElement('a')
  link.style.display = 'none'
  link.href = objectUrl
  link.download = name // 自定义文件名
  link.click() // 下载文件
  URL.revokeObjectURL(objectUrl) // 释放内存
}

// 下载文件
export function downLoadPaper(data, headers, type) {
  // console.log(data)
  if (!data) {
    return
  }
  // console.log(headers, decodeURI(headers.filename))
  const blob = new Blob([data], { type: type }),
    objectUrl = URL.createObjectURL(blob), // 创建URL
    link = document.createElement('a')
  link.style.display = 'none'
  link.href = objectUrl
  link.download = headers ? decodeURI(headers.filename) : '' // 自定义文件名
  link.click() // 下载文件
  URL.revokeObjectURL(objectUrl) // 释放内存
}
export function addDate() {
  const nowDate = new Date(),
    date = {
      year: nowDate.getFullYear(),
      month: nowDate.getMonth() + 1,
      date: nowDate.getDate()
    },
    newmonth = date.month > 10 ? date.month : '0' + date.month,
    day = date.date > 10 ? date.date : '0' + date.date,
    time = date.year + '-' + newmonth + '-' + day
  return time
}
